using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace WcfService1.DTO
{
    [DataContract]
    public class DeliveryOrderElseDTO : INotifyPropertyChanged
    {
        private String _Address;
        private CostCenterDTO _CostCenter;
        private CustomerDTO _Customer;
        private String _CustomerName;
        private DateTime? _DODate;
        private String _DONo;
        private String _DeliveryAddress;
        private ObservableCollection<DeliveryOrderElseDetailDTO> _DeliveryOrderDetails;
        private DeliveryTypeDTO _DeliveryType;
        private String _Description;
        private int _No;
        private SalesOrderDTO _SalesOrder;
        private Status _Status;
        private String _Telp;
        private String _created_by;
        private DateTime? _created_date;
        private String _modified_by;
        private DateTime? _modified_date;

        public DeliveryOrderElseDTO()
        {
            DODate = DateTime.Now.Date;
        }

        [DataMember]
        public String DONo
        {
            get { return _DONo; }
            set
            {
                _DONo = value;
                RaisePropertyChanged("DONo");
            }
        }


        [DataMember]
        public DateTime? DODate
        {
            get { return _DODate; }
            set
            {
                _DODate = value;
                RaisePropertyChanged("DODate");
            }
        }


        [DataMember]
        public SalesOrderDTO SalesOrder
        {
            get { return _SalesOrder; }
            set
            {
                _SalesOrder = value;
                RaisePropertyChanged("SalesOrder");
            }
        }


        [DataMember]
        public CustomerDTO Customer
        {
            get { return _Customer; }
            set
            {
                _Customer = value;
                RaisePropertyChanged("Customer");
            }
        }


        [DataMember]
        public DeliveryTypeDTO DeliveryType
        {
            get { return _DeliveryType; }
            set
            {
                _DeliveryType = value;
                RaisePropertyChanged("DeliveryType");
            }
        }


        [DataMember]
        public String DeliveryAddress
        {
            get { return _DeliveryAddress; }
            set
            {
                _DeliveryAddress = value;
                RaisePropertyChanged("DeliveryAddress");
            }
        }


        [DataMember]
        public String Description
        {
            get { return _Description; }
            set
            {
                _Description = value;
                RaisePropertyChanged("Description");
            }
        }


        [DataMember]
        public ObservableCollection<DeliveryOrderElseDetailDTO> DeliveryOrderDetails
        {
            get { return _DeliveryOrderDetails; }
            set
            {
                if (value == DeliveryOrderDetails) return;
                _DeliveryOrderDetails = value;
                RaisePropertyChanged("DeliveryOrderDetails");
                RefreshNumber();
            }
        }


        [DataMember]
        public String created_by
        {
            get { return _created_by; }
            set
            {
                _created_by = value;
                RaisePropertyChanged("created_by");
            }
        }


        [DataMember]
        public DateTime? created_date
        {
            get { return _created_date; }
            set
            {
                _created_date = value;
                RaisePropertyChanged("created_date");
            }
        }

        [DataMember]
        public String modified_by
        {
            get { return _modified_by; }
            set
            {
                _modified_by = value;
                RaisePropertyChanged("modified_by");
            }
        }


        [DataMember]
        public DateTime? modified_date
        {
            get { return _modified_date; }
            set
            {
                _modified_date = value;
                RaisePropertyChanged("modified_date");
            }
        }

        [DataMember]
        public String CustomerName
        {
            get { return _CustomerName; }
            set
            {
                _CustomerName = value;
                RaisePropertyChanged("CustomerName");
            }
        }


        [DataMember]
        public String Address
        {
            get { return _Address; }
            set
            {
                _Address = value;
                RaisePropertyChanged("Address");
            }
        }


        [DataMember]
        public String Telp
        {
            get { return _Telp; }
            set
            {
                _Telp = value;
                RaisePropertyChanged("Telp");
            }
        }

        [DataMember]
        public CostCenterDTO CostCenter
        {
            get { return _CostCenter; }
            set
            {
                _CostCenter = value;
                RaisePropertyChanged("CostCenter");
            }
        }

        [DataMember]
        public Status Status
        {
            get { return _Status; }
            set
            {
                _Status = value;
                RaisePropertyChanged("Status");
            }
        }

        public int No
        {
            get { return _No; }
            set
            {
                _No = value;
                RaisePropertyChanged("No");
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void RefreshNumber()
        {
            DeliveryOrderDetails.CollectionChanged += DeliveryOrderDetails_CollectionChanged;
            int number = 0;
            foreach (DeliveryOrderElseDetailDTO detailDto in DeliveryOrderDetails)
            {
                number += 1;
                detailDto.No = number;
            }
        }

        private void DeliveryOrderDetails_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            int number = 0;
            foreach (DeliveryOrderElseDetailDTO detailDto in DeliveryOrderDetails)
            {
                number += 1;
                detailDto.No = number;
            }
        }

        protected void RaisePropertyChanged(string propertyname)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyname));
        }
    }
}